{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2a04a6ba",
   "metadata": {},
   "source": [
    "* 2023-11-8\n",
    "* 课程：Python语言\n",
    "* week08"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66cfbadb",
   "metadata": {},
   "source": [
    "# 1. 本周内容\n",
    "\n",
    "## 1. 列表的数据处理（列表的方法）\n",
    "> 1. 查询数据\n",
    ">> 1. index()按照索引值查询数据，并取出值\n",
    ">> 2. slice切片方法取值\n",
    ">> 3. in方法查询元素是否存在\n",
    "> 2. 增加数据\n",
    ">> 1. append()增加在末尾\n",
    ">> 2. insert()指定位置增加\n",
    "> 3. 删除数据\n",
    ">> 1. pop()指定位置删除\n",
    ">> 2. remove()指定结果（值）删除\n",
    ">> 3. clear()青空列表\n",
    "> 4. 修改数据\n",
    ">> 1. 找到index进行修改\n",
    "> 5. 其他方法\n",
    ">> 1. 排序：sort()、reverse()\n",
    ">> 2. 复制：copy()\n",
    "\n",
    "## 2. 元组\n",
    "\n",
    "## 3. 列表实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e09398a5",
   "metadata": {},
   "source": [
    "### 1.1列表的删除\n",
    "\n",
    "> 1.pop()\n",
    ">> 1. 默认删除列表中的最后一位数据\n",
    ">> 2. 指定位置进行删除\n",
    ">> 3. 注意事项：避免index error的出现，先检查列表长度：len()\n",
    "\n",
    "> 2.remove()\n",
    ">> 1. 指定结果(值）删除\n",
    ">> 2. 注意事项：避免value error的出现，先检查元素是否存在：x in list\n",
    "\n",
    "> 3.clear()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9a9b5b2",
   "metadata": {},
   "source": [
    "### 1. pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ca252a6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ab9b4cdc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "400a4739",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李晓明'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7235e876",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李晓明'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3085b034",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "416e3fd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ab2b443b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '小红']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0317ba3",
   "metadata": {},
   "source": [
    "### 2. remove()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "68cf5e0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "15c61cdf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7b5ee8cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '小红', '李晓明']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value = name_list[1]\n",
    "name_list.remove(value)\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c7e68682",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '小红']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.remove(\"李晓明\")\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2c57e3ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先查询是否存在读值\n",
    "\"张三\" in name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2b91b67",
   "metadata": {},
   "source": [
    "### 3. clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ba16d73b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b80fea01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function clear:\n",
      "\n",
      "clear() method of builtins.list instance\n",
      "    Remove all items from list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.clear)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "73fe6ba5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.clear()\n",
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "239fcc02",
   "metadata": {},
   "source": [
    "## 2.排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d23f2fc3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 17, 19, 20, 21]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list = [18,19,18,17,19,20,21]\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2f33a472",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[17, 18, 18, 19, 19, 20, 21]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.sort()\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "71460f3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[21, 20, 19, 19, 18, 18, 17]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.reverse()\n",
    "age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d197d74",
   "metadata": {},
   "source": [
    "## 3.复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "690617c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n",
      "4606451328\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)\n",
    "print(id(name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "077f76ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4606451328\n"
     ]
    }
   ],
   "source": [
    "存档_name_list = name_list\n",
    "存档_name_list\n",
    "print(id(存档_name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d1d224c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明', 'Jalex']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.append(\"Jalex\")\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "0329b493",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明', 'Jalex']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 问：存档_name_list 的结果会不会改变？\n",
    "存档_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7b20a298",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n",
      "4606287488\n"
     ]
    }
   ],
   "source": [
    "# 综上，并不能真正的肤质，接下来我们看一卡copy\n",
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)\n",
    "print(id(name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0b9210ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4606201024\n"
     ]
    }
   ],
   "source": [
    "copy_name_list = name_list.copy()\n",
    "copy_name_list\n",
    "print(id(copy_name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b86857ff",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明', 'Jalex']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.append(\"Jalex\")\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cac55b6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "copy_name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a496ac6e",
   "metadata": {},
   "source": [
    "# 2. 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "40037eaa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12345, 54321, 'hello')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "t = 12345,54321,\"hello\"\n",
    "print(t)\n",
    "print(type(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "4a40a4a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12345, 54321, 'hello')"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (12345,54321,\"hello\")\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "73a31b2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "90969bd6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12345, 54321)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "bf4f5dea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.index(54321)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "efa73f8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.count(12345)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "ab552d7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2023, tm_mon=11, tm_mday=8, tm_hour=11, tm_min=32, tm_sec=1, tm_wday=2, tm_yday=312, tm_isdst=0)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 案例：\n",
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ed7b39b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1699414469.190204"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.time()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01fbd73a",
   "metadata": {},
   "source": [
    "# 实践\n",
    "\n",
    "购物车程序项目：\n",
    "\n",
    "```\n",
    "    要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "         2、让用户输入商品编号进行商品的购买\n",
    "         3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "         4、用户可以随时退出购买，退出时打印用户已购买的商品和支付宝余额。\n",
    "```\n",
    "\n",
    "```\n",
    "1. alipay = input()\n",
    "\n",
    "商品列表 = [\n",
    "\n",
    "]\n",
    "购物车 = []\n",
    "\n",
    "print(商品列表）\n",
    "\n",
    "2. user_ choose = input()\n",
    "\n",
    "if 商品价格 > alipay\n",
    "    1.\n",
    "else:\n",
    "    2.\n",
    "    \n",
    "3. 购物车.append(购买到的商品）\n",
    "\n",
    "4.\n",
    "\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2bbc330",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "alipay = input(\"请输入你的余额：\")\n",
    "\n",
    "商品列表 = [\n",
    "    ('iphone',5800),\n",
    "    ('Macbook Pro',10800),\n",
    "    ('ipad',2800),\n",
    "    ('iwatch',2000),\n",
    "]\n",
    "\n",
    "print(\"--------苹果专卖店--------\")\n",
    "for i in 商品列表:\n",
    "    print(\"商品编号：%s，商品名称：%s，商品价格：%s 元\"%(商品列表.index(i),i[0],i[1]))\n",
    "\n",
    "# 2、让用户输入商品编号进行商品的购买\n",
    "# 3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "user_choose = input(\"请选择要买的商品，输入商品编号：\")\n",
    "# 输出的user_choice不能是str，因此int将user_choice转为整数。\n",
    "if 商品列表[int(user_choose)][1] < int(alipay):\n",
    "    alipay = int(alipay) - 商品列表[int(user_choose)][1]\n",
    "    print(\"%s 已购买成功，您的余额还有：%s 元。\"%(商品列表[int(user_choose)][0],alipay))\n",
    "else:\n",
    "    print(\"您的余额只剩%s,不能购买%s商品。\"%(alipay,商品列表[int(user_choose)][0]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
